package hdf5.parser;

import ncsa.hdf.object.Dataset;
import ncsa.hdf.object.h5.H5File;
import ncsa.hdf.object.h5.H5ScalarDS;

import java.util.logging.Logger;

public class HDF5Parser {

    private static final Logger logger = Logger.getLogger("hdf5.parser.HDF5Parser");

    public double[][] readHDF5Array(String fileName, String dataSetName) {
        double[][] data = null;
        try {
            H5File file = new H5File(fileName);
            file.open();
            Dataset dataset = new H5ScalarDS(file, "", dataSetName);
            dataset.open();
            dataset.init();
            long[] dimensions = dataset.getDims();
            int sizeTime = (int) dimensions[1];
            int sizeMesh = (int) dimensions[0];
            data = new double[sizeMesh][];
            float[] tempData = (float[]) dataset.getData();
            for (int i = 0; i < sizeMesh; i++) {
                data[i] = new double[sizeTime];
                for (int k = 0; k < sizeTime; k++) {
                    data[i][k] = (double) tempData[i * sizeTime + k];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}
